/*
 * Queue.cpp
 *
 *  Created on: 2012-04-12
 *      Author: tony
 */

#include <assert.h>
#include "Queue.h"

namespace graph {

template<class T>
Queue<T>::Queue(): front(NULL), back(NULL) {
}

template<class T>
Queue<T>::~Queue() {
	while (back != NULL)
		dequeue();
	front = back = NULL;
}

template<class T>
void Queue<T>::enqueue( T d ){
	if (back == NULL)
		front = back = new Node<T>( d, NULL, NULL );
	else{
		back = new Node<T>( d, back, NULL );
		back->getNext()->setPrev( back );
	}
}

template<class T>
T Queue<T>::dequeue(){
	assert( front != NULL );
	Node<T>* result = front;

	if (front == back) {
		front = back = NULL;
	} else {
		front = front->getPrev();
		front->setNext( NULL );
	}
	T d = result->get();
	delete result;
	return d;
}

template<class T>
bool Queue<T>::is_empty(){
	return (front == NULL);
}

} /* namespace graph */
